package luceneUtils;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class HighLight {
	private String indexDir="lucene/index01";
	public static String highlight(Query query,String filedname,String text){
        try {
            QueryScorer queryScorer=new QueryScorer(query);
            Fragmenter fragmenter=new SimpleSpanFragmenter(queryScorer);
            Formatter formatter=new SimpleHTMLFormatter("<font color=\"green\">", "</font>");
            Highlighter highlighter=new Highlighter(formatter,queryScorer);
            highlighter.setTextFragmenter(fragmenter);
            String str=highlighter.getBestFragment(new IKAnalyzer(), filedname, text);
            return str;
        }  catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
        return text;
    }
}
